<?php
namespace app\demo\controller;

use app\validate\BookCheck;
use my\Sendsms;
use my\smtp;
use my\Captcha;

class Demo extends Base
{

    // 参数验证, (主要是字段长度)
    private function validateParam() {
        $post = [
            'title' => 'Hello,How are you!!',
            'age' => 120,
            'num' => -1,
            'price' => '32151611212.365',
            'email' => 'aaa@qq.com',
            'bool' => true,
            'username' => 'jiang2251',
            'tel' => '19526860284',
            'create_time' => '9-06-28',
        ];
        $book_check = new BookCheck();
        var_dump(validate_param($book_check,$post));
    }

    // 获取图形验证码
    public function getCaptcha() {
        $cache = new Captcha(['type'=>0,'output'=>2]);
        $res = $cache->create();
        echo '<img src="'.$res['base64_prefix'].$res['base64'].'">';
        halt($res);
    }

    // 验证图形验证码
    public function checkCaptcha() {
        $param = request();
        $captcha = new Captcha(['type'=>0,'output'=>2]);
        var_dump($captcha->check($param['code'], $param['captcha_key']));
    }

    // 发送邮件 (异地登录提醒)
    public function sendEmail() {
        $param = [
            'email' => '1873645345@qq.com',
            'plat' => 'PSN玩家',
            'username' => 'root',
            'province' => '杭州',
        ];

        try {
            $html =
                <<<EOT
<!doctype html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>账号安全提醒</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .container{
            width: 100%;
            display: flex;
            flex-direction: column;
            background: #ffffff;
        }
    </style>
</head>
<body>
<div class="container">
    <div class="box">
        <p class="title">{$param['plat']} 提醒您!</p>
        <p>您的账号 <span style="color: red">{$param['username']}</span> 正在异地登录, 登录地点为 <span style="color: red">{$param['province']}</span>, 如非本人操作请尽快修改密码! </p>
    </div>
</div>
</body>
</html>
EOT;

            $mailbody = $html;
//使用163邮箱服务器
            $smtpserver = env('smtp.smtpserver','');
//163邮箱服务器端口
            $smtpserverport = 465;
//你的163服务器邮箱账号
            $smtpusermail = env('smtp.smtpusermail','');
//收件人邮箱
            $smtpemailto = $param['email'];
//你的邮箱账号(去掉@163.com)
            $smtpuser = env('smtp.smtpuser');//你的163邮箱去掉后面的163.com
//你的邮箱密码
            $smtppass = env('smtp.smtppass'); //你的163邮箱SMTP的授权码，千万不要填密码！！！
//邮件主题
            $mailsubject = "新账号安全提醒";
//邮件内容
//邮件格式（HTML/TXT）,TXT为文本邮件
            $mailtype = "HTML";

//这里面的一个true是表示使用身份验证,否则不使用身份验证.
            $smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);
//是否显示发送的调试信息
            $smtp->debug = false;
//发送邮件
            $smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
        } catch (\Exception $e) {
//            mylog($this->cmd,$e->getMessage(),'email.log');
            return errorReturn($e->getMessage(),[
                'file' => $e->getFile(),
                'line' => $e->getLine(),
            ]);
        }
        exit('success');
    }

    // 发送短信
    private function send_sms() {
        $sms = new Sendsms();
        $code = mt_rand(1000,9999);
        $tel = "13102163019";
        $sms_data['tpl_code'] = 'SMS_126362322';
        $sms_data['tel'] = $tel;
        $sms_data['param'] = [
            'code' => $code
        ];
        $res = $sms->send($sms_data);
        if($res->Code === 'OK') {
            // todo 短信发送成功!
            pp($res);
        }else {
            pp($res->Message);
            // todo 失败
        }
    }






}
